package com.cxb.servicemap.remote;

import com.cxb.internalcommon.constant.AmapConfigConstants;
import com.cxb.internalcommon.dto.ResponseResult;
import com.cxb.internalcommon.request.PointDTO;
import com.cxb.internalcommon.request.PointRequestDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

@Service
public class PointClient {


    @Value("${amap.key}")
    private String amapKey;
    @Value("${amap.sid}")
    private String amapSid;
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 上传轨迹点
     * @param pointRequestDTO
     * @return
     */
    public ResponseResult upload(PointRequestDTO pointRequestDTO){

        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.POINTS_UPLOAD);
        url.append("?");
        url.append("key="+amapKey);
        url.append("&");
        url.append("sid="+amapSid);
        url.append("&");
        url.append("tid="+pointRequestDTO.getTid());
        url.append("&");
        url.append("trid="+pointRequestDTO.getTrid());
        url.append("&");
        url.append("points=");
        PointDTO[] points = pointRequestDTO.getPoints();
        url.append("%5B");
        for(PointDTO p:points) {
            url.append("%7B");
            String locatetime = p.getLocatetime();
            String location = p.getLocation();

            url.append("%22location%22");
            url.append("%3A");
            url.append("%22"+location+"%22");
            url.append("%2C");

            url.append("%22locatetime%22");
            url.append("%3A");
            url.append(locatetime);

            url.append("%7D");
        }
        url.append("%5D");
        System.out.println("上传位置请求："+url.toString());
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(URI.create(url.toString()),null, String.class);
        System.out.println("上传位置响应："+stringResponseEntity.getBody());
        return ResponseResult.success("");
    }
}
